﻿Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Runtime.InteropServices

Namespace System.Workflow.ComponentModel.Design
    Public Class ActivityPreviewDesignerTheme
        Inherits CompositeDesignerTheme
        ' Methods
        Public Sub New(ByVal theme As WorkflowTheme)
            MyBase.New(theme)
            Me.previewForeColor = Color.WhiteSmoke
            Me.previewBackColor = Color.White
            Me.previewBorderColor = Color.Gray
        End Sub

        Private Sub ApplySystemColors()
            Me.PreviewForeColor = SystemColors.ButtonFace
            Me.PreviewBackColor = SystemColors.Window
            Me.PreviewBorderColor = SystemColors.ControlDarkDark
            Me.BorderColor = SystemColors.ControlDarkDark
        End Sub

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try 
                If (Not Me.previewForegroundBrush Is Nothing) Then
                    Me.previewForegroundBrush.Dispose
                    Me.previewForegroundBrush = Nothing
                End If
                If (Not Me.previewBackgroundBrush Is Nothing) Then
                    Me.previewBackgroundBrush.Dispose
                    Me.previewBackgroundBrush = Nothing
                End If
                If (Not Me.previewBorderPen Is Nothing) Then
                    Me.previewBorderPen.Dispose
                    Me.previewBorderPen = Nothing
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub

        Public Overrides Sub Initialize()
            MyBase.Initialize
            If MyBase.ContainingTheme.AmbientTheme.UseOperatingSystemSettings Then
                Me.ApplySystemColors
            End If
        End Sub

        Public Overrides Sub OnAmbientPropertyChanged(ByVal ambientProperty As AmbientProperty)
            MyBase.OnAmbientPropertyChanged(ambientProperty)
            If (ambientProperty = AmbientProperty.DesignerSize) Then
                Me.PreviewBorderColor = Me.previewBorderColor
            ElseIf (ambientProperty = AmbientProperty.OperatingSystemSetting) Then
                Me.ApplySystemColors
            End If
        End Sub


        ' Properties
        <SRDescription("PreviewBackColorDesc", "System.Workflow.ComponentModel.Design.DesignerResources"), SRCategory("BackgroundCategory", "System.Workflow.ComponentModel.Design.DesignerResources"), Editor(GetType(ColorPickerEditor), GetType(UITypeEditor)), TypeConverter(GetType(ColorPickerConverter)), DispId(14)> _
        Public Property PreviewBackColor As Color
            Get
                Return Me.previewBackColor
            End Get
            Set(ByVal value As Color)
                If MyBase.ReadOnly Then
                    Throw New InvalidOperationException(DR.GetString("ThemePropertyReadOnly", New Object(0  - 1) {}))
                End If
                Me.previewBackColor = value
                If (Not Me.previewBackgroundBrush Is Nothing) Then
                    Me.previewBackgroundBrush.Dispose
                    Me.previewBackgroundBrush = Nothing
                End If
            End Set
        End Property

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(False)> _
        Friend ReadOnly Property PreviewBackgroundBrush As Brush
            Get
                If (Me.previewBackgroundBrush Is Nothing) Then
                    Me.previewBackgroundBrush = New SolidBrush(Me.previewBackColor)
                End If
                Return Me.previewBackgroundBrush
            End Get
        End Property

        <TypeConverter(GetType(ColorPickerConverter)), SRCategory("ForegroundCategory", "System.Workflow.ComponentModel.Design.DesignerResources"), Editor(GetType(ColorPickerEditor), GetType(UITypeEditor)), DispId(15), SRDescription("PreviewBorderColorDesc", "System.Workflow.ComponentModel.Design.DesignerResources")> _
        Public Property PreviewBorderColor As Color
            Get
                Return Me.previewBorderColor
            End Get
            Set(ByVal value As Color)
                If MyBase.ReadOnly Then
                    Throw New InvalidOperationException(DR.GetString("ThemePropertyReadOnly", New Object(0  - 1) {}))
                End If
                Me.previewBorderColor = value
                If (Not Me.previewBorderPen Is Nothing) Then
                    Me.previewBorderPen.Dispose
                    Me.previewBorderPen = Nothing
                End If
            End Set
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Friend ReadOnly Property PreviewBorderPen As Pen
            Get
                If (Me.previewBorderPen Is Nothing) Then
                    Me.previewBorderPen = New Pen(Me.previewBorderColor, CSng(MyBase.BorderWidth))
                End If
                Return Me.previewBorderPen
            End Get
        End Property

        <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Friend ReadOnly Property PreviewButtonSize As Size
            Get
                Return ActivityPreviewDesignerTheme.PreviewButtonSizes(CInt(MyBase.ContainingTheme.AmbientTheme.DesignerSize))
            End Get
        End Property

        <DispId(13), TypeConverter(GetType(ColorPickerConverter)), SRDescription("PreviewForeColorDesc", "System.Workflow.ComponentModel.Design.DesignerResources"), SRCategory("ForegroundCategory", "System.Workflow.ComponentModel.Design.DesignerResources"), Editor(GetType(ColorPickerEditor), GetType(UITypeEditor))> _
        Public Property PreviewForeColor As Color
            Get
                Return Me.previewForeColor
            End Get
            Set(ByVal value As Color)
                If MyBase.ReadOnly Then
                    Throw New InvalidOperationException(DR.GetString("ThemePropertyReadOnly", New Object(0  - 1) {}))
                End If
                Me.previewForeColor = value
                If (Not Me.previewForegroundBrush Is Nothing) Then
                    Me.previewForegroundBrush.Dispose
                    Me.previewForegroundBrush = Nothing
                End If
            End Set
        End Property

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(False)> _
        Friend ReadOnly Property PreviewForegroundBrush As Brush
            Get
                If (Me.previewForegroundBrush Is Nothing) Then
                    Me.previewForegroundBrush = New SolidBrush(Me.previewForeColor)
                End If
                Return Me.previewForegroundBrush
            End Get
        End Property

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(False)> _
        Friend ReadOnly Property PreviewItemCount As Integer
            Get
                Return 5
            End Get
        End Property

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(False)> _
        Friend ReadOnly Property PreviewItemSize As Size
            Get
                Return ActivityPreviewDesignerTheme.ItemSizes(CInt(MyBase.ContainingTheme.AmbientTheme.DesignerSize))
            End Get
        End Property

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(False)> _
        Friend ReadOnly Property PreviewWindowSize As Size
            Get
                Return ActivityPreviewDesignerTheme.PreviewWindowSizes(CInt(MyBase.ContainingTheme.AmbientTheme.DesignerSize))
            End Get
        End Property


        ' Fields
        Private Const DefaultItemCount As Integer = 5
        Friend Shared ReadOnly EditButtonImage As Bitmap = [TryCast](DR.GetImage("EditModeIcon"),Bitmap)
        Private Shared ReadOnly ItemSizes As Size() = New Size() { New Size(20, 20), New Size(20, 20), New Size(30, 30) }
        Friend Shared ReadOnly LeftScrollImage As Bitmap = [TryCast](DR.GetImage("MoveLeft"),Bitmap)
        Friend Shared ReadOnly LeftScrollImageUp As Bitmap = [TryCast](DR.GetImage("MoveLeftUp"),Bitmap)
        Private previewBackColor As Color
        Private previewBackgroundBrush As Brush
        Private previewBorderColor As Color
        Private previewBorderPen As Pen
        Friend Shared ReadOnly PreviewButtonImage As Bitmap = [TryCast](DR.GetImage("PreviewModeIcon"),Bitmap)
        Private Shared ReadOnly PreviewButtonSizes As Size() = New Size() { New Size(&H10, &H10), New Size(&H10, &H10), New Size(20, 20) }
        Private previewForeColor As Color
        Private previewForegroundBrush As Brush
        Friend Shared ReadOnly PreviewImage As Bitmap = [TryCast](DR.GetImage("PreviewIndicator"),Bitmap)
        Private Shared ReadOnly PreviewWindowSizes As Size() = New Size() { New Size(&HAC, 120), New Size(&HAC, 120), New Size(&HD4, 160) }
        Friend Shared ReadOnly RightScrollImage As Bitmap = [TryCast](DR.GetImage("MoveRight"),Bitmap)
        Friend Shared ReadOnly RightScrollImageUp As Bitmap = [TryCast](DR.GetImage("MoveRightUp"),Bitmap)
    End Class
End Namespace

